package com.read.scriptures.util;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ProgressBar;

import com.read.scriptures.app.HuDongApplication;
import com.read.scriptures.config.SystemConfig;
import com.read.scriptures.db.DatabaseHelper;
import com.read.scriptures.model.Baike;
import com.read.scriptures.model.Bookmark;
import com.read.scriptures.model.Category;
import com.read.scriptures.model.Chapter;
import com.read.scriptures.model.SearchTemp;
import com.read.scriptures.model.Spirituality;
import com.read.scriptures.model.Volume;
import com.read.scriptures.util.ThreadUtil.ProgressThread;
import com.zxl.common.db.sqlite.DbException;
import com.zxl.common.db.sqlite.Selector;
import com.zxl.common.db.sqlite.WhereBuilder;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import taobe.tec.jcc.JChineseConvertor;

/**
 * Created by LGM. Datetime: 2015/7/4. Email: lgmshare@mgail.com
 */
public class SearchTextUtil {

    public static boolean searchByKeywordLoadFinish = false;
    public static int searchLimit = 50;

    /**
     * 搜索书籍关键字
     *
     * @param volumeList
     * @param keyword
     * @return
     */
    public static List<Bookmark> searchVolumeByKeyword(List<Volume> volumeList, String keyword,   ProgressBar progressBar) {
        int length = volumeList.size();
        progressBar.setMax(length);
        progressBar.setProgress(0);
        List<Bookmark> bookmarkList = new ArrayList<Bookmark>();
        for (int i = 0; i < length; i++) {
            Volume volume = volumeList.get(i);
            String macthLine = textMacth(volume.getVolName().replaceAll("^\\d{1,}-", ""), keyword);
            if (macthLine != null) {
                Bookmark mark = new Bookmark();
                mark.setCategroyId(String.valueOf(volume.getCategoryId()));
                mark.setVolumeId(volume.getId());
                mark.setVolumeName(volume.getVolName().replaceAll("^\\d{1,}-", ""));
                mark.setChapterCount(volume.getChpCount());
                mark.setIndex(0);
                mark.setType(0);
                mark.setContent(macthLine);
                mark.setCategroyId(volume.getCategoryId() + "");
                bookmarkList.add(mark);
            }
            progressBar.setProgress(i);
        }
        return bookmarkList;
    }

    /**
     * 搜索目录关键字
     *
     * @param chapterList
     * @param keyword
     * @return
     */
    public static List<Bookmark> searchChapterByKeyword(List<Chapter> chapterList, String keyword, ProgressBar progressBar) {
        int length = chapterList.size();
        progressBar.setMax(length);
        progressBar.setProgress(0);
        List<Bookmark> bookmarkList = new ArrayList<Bookmark>();
        for (int i = 0; i < length; i++) {
            Chapter chapter = chapterList.get(i);
            String macthLine = textMacth(chapter.getShowName(), keyword);
            if (macthLine != null) {
                Bookmark bookmark = new Bookmark();
                bookmark.setVolumeId(chapter.getVolumeId());
                bookmark.setVolumeName(chapter.getShowVolumeName());
                bookmark.setChapterName(chapter.getShowName());
                bookmark.setChapterIndexId(chapter.getIndexId());
                bookmark.setChapterCount(chapter.getChapterCount());
                bookmark.setIndex(0);
                bookmark.setType(0);
                bookmark.setContent(macthLine);
                bookmark.setCategroyId(chapter.getCategoryId() + "");
                bookmarkList.add(bookmark);
            }
            progressBar.setProgress(i);
        }
        return bookmarkList;
    }

    /**
     * 搜索标题关键字
     *
     * @param keyword
     * @param progressBar
     * @param searchMap
     * @return
     */
    public static List<Bookmark> searchContentByKeyword(String keyword, ProgressBar progressBar, Map<String, Object> searchMap) {
        List<Volume> volumeList = (List<Volume>) searchMap.get("volumeList");
        Volume volume = (Volume) searchMap.get("volume");
        Category category = (Category) searchMap.get("category");
        String rootId = (String) searchMap.get("rootId");
        long start = System.currentTimeMillis();
        boolean searchTitle = (Boolean) searchMap.get("searchTitle");
        int searchLength = volumeList.size();
        progressBar.setMax(searchLength * 100);
        progressBar.setProgress(0);
        List<Bookmark> bookmarkResultList = new ArrayList<>();
        List<SearchTemp> list = null;
        try {
            if (volume != null) {
                list = HuDongApplication.getInstance().getDbUtils().findAll(Selector.from(Chapter.class)
                        .where(WhereBuilder.getInstance("volumeId", "=", volume.getId())).toString(), SearchTemp.class);
            } else if (category != null) {
                list = HuDongApplication.getInstance().getDbUtils()
                        .findAll(Selector.from(Chapter.class)
                                .where(WhereBuilder.getInstance("categoryId", "=", category.getId())).toString(), SearchTemp.class);
            } else {
                String sql;
                if(rootId.contains("(")){
                    sql = "SELECT * FROM " + DatabaseHelper.TABLE_CHAPTER + " WHERE parentId in " + rootId;
                } else {
                    sql = "SELECT * FROM " + DatabaseHelper.TABLE_CHAPTER + " WHERE parentId = " + rootId;
                }
                LogUtil.test("搜索关键字sql：" + sql);
                list = HuDongApplication.getInstance().getDbUtils().findAll(sql, SearchTemp.class);
            }
            LogUtil.test("搜索关键字搜索耗时：" + (System.currentTimeMillis() - start));
            List<String> have = new ArrayList<>();
            if (list != null) {
                int progress = 0;
                progressBar.setMax(list.size());
                for (SearchTemp chapter : list) {
                    try {
                        int position = 0;
                        String content = chapter.getContent();
                        if (content == null) {
                            continue;
                        }
                        String[] keywords = keyword.split(" ");
                        boolean isContains = true;
                        for (int i = 0; i < keywords.length; i++) {
                            if (!content.contains(keywords[i])) {
                                isContains = false;
                                break;
                            }
                        }
                        if (isContains) {
                            String volName = "";
                            for (Volume volumetemp : volumeList) {
                                if (chapter.getVolumeId() == volumetemp.getId()) {
                                    volName = volumetemp.getVolName().replaceAll("^\\d{1,}-", "");
                                }
                            }
                            List<String> contentList = new ArrayList<String>(Arrays.asList
                                    (content.split("\n")));
                            int remove = 0;
                            for (int j = 0; j < contentList.size(); j++) {
                                String line = contentList.get(j);
                                if (line.trim().equals("\n") || line.trim().equals("\n\r")
                                        || StringUtil.isEmpty(line.trim())) {
                                    remove++;
                                    continue;
                                }

                                boolean isBreak = false;
                                isContains = false;
                                for (int i = 0; i < keywords.length; i++) {
                                    if (isBreak) {
                                        break;
                                    }
                                    if (line.contains(keywords[i])) {
                                        if (searchTitle) {
                                            if (line.contains("<b")) {
                                                Document document = Jsoup.parse(line);
                                                Elements bs = document.getElementsByTag("b");
                                                for (Element element : bs) {
                                                    if (element.text().contains(keywords[i])) {
                                                        isContains = true;
                                                        isBreak = true;
                                                    }
                                                }
                                            } else if (line.contains("<h3")) {
                                                isContains = true;
                                                isBreak = true;
                                            } else if (line.contains("<h6")) {
                                                isContains = true;
                                                isBreak = true;
                                            }
                                        } else {
                                            if (line.contains("<b")) {
                                                isContains = false;
                                                isBreak = true;
                                            } else {
                                                isContains = true;
                                                isBreak = true;
                                            }
                                        }
                                    }
                                }
                                if (isContains) {
                                    position = j - remove + 1;
                                    Bookmark point = new Bookmark();
                                    point.setVolumeId(chapter.getVolumeId());
                                    point.setVolumeName(volName);
                                    point.setChapterName(chapter.getName());
                                    point.setChapterIndexId(chapter.getIndexId());
                                    point.setChapterCount(chapter.getChapterCount());
                                    point.setIndex(position);
                                    point.setCategroyId(chapter.getCategoryId() + "");
                                    point.setType(0);
                                    String pointContent = textMacth(line, keyword);
                                    if (StringUtil.isEmpty(pointContent)) {
                                        continue;
                                    }
                                    point.setContent(pointContent);
                                    boolean isHave = false;
                                    for (Bookmark bookmark : bookmarkResultList) {
                                        if (bookmark.getContent().equals(pointContent) && bookmark.getChapterIndexId() == chapter.getIndexId()
                                                && bookmark.getVolumeId() == chapter.getVolumeId()) {
                                            isHave = true;
                                        }
                                    }
                                    if (!isHave)
                                        bookmarkResultList.add(point);
                                }
                            }
                            if (have.indexOf(chapter.getName()) >= 0) {
                                continue;
                            }
                            have.add(chapter.getName());
                        }
                    } catch (Exception e) {
                        LogUtil.error("Exception", e);
                    } finally {
                        progress++;
                        progressBar.setProgress(progress);
                    }

                }
            }
        } catch (DbException e) {
            LogUtil.error("DbException", e);
        }
        LogUtil.test("搜索关键字搜索耗时：" + (System.currentTimeMillis() - start));
        return bookmarkResultList;
    }

    /**
     * 搜索内容关键字
     *
     * @param volumeList   书籍列表
     * @param nodeCategory 选中的分类
     * @param volume       选中的书籍
     * @param keyword      搜索关键字
     * @param ids       搜索的分类
     * @param offset       偏移量
     * @return
     */
    public static List<Bookmark> searchContent(List<Volume> volumeList, Category nodeCategory, Volume volume, String keyword, int cateId, List<Integer> ids, int offset, int count) {
        String[] keys = keyword.trim().split(" ");
        StringBuilder stringBuilder = new StringBuilder();

        if (volume != null) {
            stringBuilder.append("select * from chapter where categoryId = " + volume.getCategoryId());
        } else if (nodeCategory != null) {
            stringBuilder.append("select * from chapter where categoryId = " + nodeCategory.getId());
        } else {
            if (ids.size() > 1) {
                String selection = "(";
                for (int i = 0; i < ids.size(); i++) {
                    selection += ids.get(i);
                    if (i < ids.size() - 1) {
                        selection += ", ";
                    }
                }
                selection += ")";
                stringBuilder.append("select * from chapter where parentId in " + selection);
            } else {
                //大分支不执行cateId,而是拼接所有小分支
                stringBuilder.append("select * from chapter where parentId =" + ids.get(0));
            }
        }
        for (String key : keys) {
            if (!TextUtils.isEmpty(key)) {
                stringBuilder.append(" and content like '%" + key + "%' ");
            }
        }
        stringBuilder.append(" and content NOT like '%【%' and content NOT like '%】%'  and  name  NOT like '%【%' and name NOT like '%】%' and  name  NOT like '%jieshao%'  limit " + offset + ",50");
        LogUtil.test("搜索内容关键字sql2：" + stringBuilder);
        List<Bookmark> bookmarkResultList = new ArrayList<>();
        List<String> versions = new ArrayList<>();
        if (cateId == 1) {
            //圣经 要筛选和合本、吕振中、思高本等版本
            versions = HuDongApplication.mVersions;
        } else if (cateId == 4) {
            //怀著 要筛选中英文
            versions = HuDongApplication.mVersions_HZ;
        }
        try {
            List<SearchTemp> all = HuDongApplication.getInstance().getDbUtils().find1(stringBuilder.toString(), SearchTemp.class);
            searchByKeywordLoadFinish = all == null || all.size() < searchLimit;
            for (int i = 0; i < all.size(); i++) {
                SearchTemp chapter = all.get(i);
                List<String> contentList = new ArrayList<>(Arrays.asList(chapter.getContent().split("\n")));
                for (int j = 0; j < contentList.size(); j++) {
                    String str = contentList.get(j);
                    boolean isContainsVersion = true;
                    boolean isMatched = false;
                    if (str.contains("〖") || str.contains("〗")) {
                        a:
                        for (String version : versions) {
                            String p = "〖(" + version + ")〗.*?〖(/" + version + ")〗";
                            Pattern P = Pattern.compile(p);
                            Matcher matcher = P.matcher(str);
                            isMatched = matcher.find();
                            if (isMatched) break a;
                        }
                    } else {
                        isContainsVersion = false;
                        isMatched = true;
                    }
                    if (isMatched) {
                        String pointContent = textMacth(str, keyword);
                        if (!StringUtil.isEmpty(pointContent)) {
                            String volName = "";
                            for (Volume volumeTemp : volumeList) {
                                if (chapter.getVolumeId() == volumeTemp.getId()) {
                                    volName = volumeTemp.getVolName().replaceAll("^\\d{1,}-", "");
                                }
                            }
                            Bookmark point = new Bookmark();
                            point.setVolumeId(chapter.getVolumeId());
                            point.setVolumeName(volName);
                            point.setChapterName(chapter.getName());
                            point.setChapterIndexId(chapter.getIndexId());
                            point.setChapterCount(chapter.getChapterCount());
                            point.setCategroyId(chapter.getCategoryId() + "");
                            int size = versions.size();
                            if (size != 0) {
                                if (!isContainsVersion) {
                                    point.setIndex(j + 1);
                                } else {
                                    point.setIndex(j / 2 + 1);
                                }
                            }
                            point.setType(0);
                            point.setContent(pointContent);
                            bookmarkResultList.add(point);
                        }
                    }
                }
            }

        } catch (DbException e) {
            e.printStackTrace();
        }
        return bookmarkResultList;
    }

    /**
     * 搜索内容关键字(灵修)
     *
     * @param context
     * @param spirituality
     * @param keyword
     * @return
     */
    public static List<Bookmark> searchSpiritualityContentByKeyword(Context context, Spirituality spirituality, String keyword) {
        List<Bookmark> points = new ArrayList<Bookmark>();
        try {
            int position = 0;
            Spirituality selectSpirituality = HuDongApplication.getInstance().getDbUtils()
                    .findById(spirituality.getClass(), spirituality.getId());
            String content = selectSpirituality.getContent();
            List<String> contentList = new ArrayList<String>(Arrays.asList(content.split("\n")));
            for (String line : contentList) {
                if (!TextUtils.isEmpty(line)) {
                    position++;
                    String macthLine = textMacth(line, keyword);
                    if (macthLine != null) {
                        Bookmark point = new Bookmark();
                        point.setChapterName(spirituality.getDaytime());
                        point.setVolumeId(spirituality.getId());
                        point.setVolumeName(spirituality.getShowName());
                        point.setCategroyName(spirituality.getPatrent());
                        point.setVolumeName(spirituality.getBook());
                        point.setIndex(position);
                        point.setContent(macthLine);
                        point.setType(1);
                        points.add(point);
                    }
                }

            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        return points;
    }

    public static boolean isLuoJiShengJing(List<String> contents) {
        if (contents != null && !contents.isEmpty()) {
            int count = 0;

            for (String content : contents) {
                if (!TextUtils.isEmpty(content) && content.length() > 12) {
                    String preTxt = content.substring(0, 12);
                    String shengJingTxt = CharUtils.match("[\\u4e00-\\u9fa5]{1,2}\\d+:\\d+", preTxt);
                    if (!TextUtils.isEmpty(shengJingTxt) && content.replace(" ", "").startsWith(shengJingTxt)) {
                        count++;
                    }
                }
            }

            if (count > 2) {
                return true;
            }
        }

        return false;
    }

    /**
     * 获取章节内容
     *
     * @param context
     * @param chapter
     * @return
     */
    public static List<String> queryChaptreContent(Context context, Chapter chapter, int textModel) {
        String contents = chapter.getContent();
        List<String> content = new ArrayList<>(Arrays.asList(contents.split("\n")));
        boolean isSheingJ = isLuoJiShengJing(content);

        // int remove = 0;
        for (int i = 0; i < content.size(); i++) {
            String string = content.get(i);

            if (string.trim().equals("\n") || string.trim().equals("\n\r") || StringUtil.isEmpty(string.trim())) {
                content.remove(i);
                i--;
                continue;
            }
            if (textModel == SystemConfig.TEXT_MODEL_FANTI) {
                content.set(i, jian2fan(string + "\n"));
            }

        }
        if (isSheingJ) {
            List<String> rContent = new ArrayList<>();
            if (content != null && !content.isEmpty()) {
                String saveHead = "";
                for (String con : content) {
                    List<String> allTags = HuDongApplication.baseVersions;
                    List<String> tags = HuDongApplication.mVersions;
                    String head = CharUtils.match("[\\u4e00-\\u9fa5]{1,2}\\d+:\\d+", con);
                    boolean isNoContains = false;
                    // 剔除没有选中的
                    for (String tagName : allTags) {
                        if (!tags.contains(tagName)) {
                            if (con.contains("〖" + tagName + "〗") && con.contains("〖/" + tagName + "〗")) {
                                isNoContains = true;
                            }
                        }
                    }
                    if (!isNoContains) {
                        if (!TextUtils.isEmpty(head)) {
                            if (!con.contains(head)) {
                                rContent.add(head + con);
                            } else {
                                rContent.add(con);
                            }
                        } else if (!TextUtils.isEmpty(saveHead)) {
                            if (!con.contains(saveHead)) {
                                rContent.add(saveHead + con);
                            } else {
                                rContent.add(con);
                            }
                            saveHead = "";
                        } else {
                            rContent.add(con);
                        }
                    } else {
                        if (!TextUtils.isEmpty(head)) {
                            saveHead = head;
                        }
                    }
                }
            }
            return rContent;
        } else {
            List<String> rContent = new ArrayList<String>();
            List<String> allTags = HuDongApplication.HZ_baseVersions;
            List<String> tags = HuDongApplication.mVersions_HZ;
            if (tags.size() != allTags.size()) {
                if (tags.get(0).contains("中文")) {
                    for (String entity : content) {
                        if (entity.contains("中文")) {
                            rContent.add(entity);
                        }
                    }
                } else { //英文
                    for (String entity : content) {
                        if (entity.contains("英文")) {
                            rContent.add(entity);
                        }
                    }
                }
                if (rContent.size() > 0) {
                    return rContent;
                }
            }

            return content;
        }
    }

    /**
     * 获取章节内容
     *
     * @param context
     */
    public static List<String> queryChaptreContentByContent(Context context, String contents, int  textModel) {
        List<String> content = new ArrayList<String>();
        content = new ArrayList<>(Arrays.asList(contents.split("\n")));
        // int remove = 0;
        for (int i = 0; i < content.size(); i++) {
            String string = content.get(i);
            if (string.trim().equals("\n") || string.trim().equals("\n\r") || StringUtil.isEmpty
                    (string.trim())) {
                content.remove(i);
                i--;
                continue;
            }
            if (textModel == SystemConfig.TEXT_MODEL_FANTI) {
                content.set(i, jian2fan(string + "\n"));
            }
        }
        return content;
    }

    /**
     * 获取每日灵修内容
     *
     * @param context
     * @param spirituality
     * @return
     */
    public static List<String> querySpiritualityContent(Context context, Spirituality  spirituality, int textModel) {
        List<String> content = new ArrayList<String>();
        LogUtil.log("spirituality:" + spirituality.getId() + "-" + spirituality.toString());
        try {
            Spirituality selectSpirituality = HuDongApplication.getInstance().getDbUtils()
                    .findById(spirituality.getClass(), spirituality.getId());
            if (selectSpirituality != null) {
                String selectContent = selectSpirituality.getContent();
                if (selectContent == null) {
                    return content;
                }
                if (textModel == SystemConfig.TEXT_MODEL_FANTI) {
                    selectContent = jian2fan(selectContent);
                }
                String[] arrays = selectContent.split("\n");
                if (arrays.length == 0) {
                    content.add(new String(""));
                    return content;
                }
                for (int i = 0; i < arrays.length; i++) {
                    if (StringUtil.isNotEmpty(arrays[i])) {
                        content.add(arrays[i]);
                    }
                }
            }
        } catch (DbException e) {
            Writer result = new StringWriter();
            PrintWriter printWriter = new PrintWriter(result);
            e.printStackTrace(printWriter);
            LogUtil.log("querySpiritualityContent exception:" + result.toString());
            LogUtil.error("querySpiritualityContent", e);
        }

        return content;
    }

    /**
     * 获取百科内容
     *
     * @param context
     * @param baike
     * @param textModel
     * @return
     */
    public static List<String> queryBaikeContent(Context context, Baike baike, int textModel) {
        List<String> content = new ArrayList<String>();
        try {
            Baike selectBaike = HuDongApplication.getInstance().getDbUtils()
                    .findById(baike.getClass(), baike.getId());
            if (selectBaike != null) {
                String selectContent = selectBaike.getContent();
                if (selectContent == null) {
                    return content;
                }
                if (textModel == SystemConfig.TEXT_MODEL_FANTI) {
                    selectContent = jian2fan(selectContent);
                }
                String[] arrays = selectContent.split("\n");
                for (int i = 0; i < arrays.length; i++) {
                    if (arrays[i].trim().equals("\n") || arrays[i].trim().equals("\n\r") ||
                            StringUtil.isEmpty(arrays[i].trim())) {
                        continue;
                    } else {
                        content.add(arrays[i]);
                    }
                }
            }
        } catch (DbException e) {
            Writer result = new StringWriter();
            PrintWriter printWriter = new PrintWriter(result);
            e.printStackTrace(printWriter);
            LogUtil.log("querySpiritualityContent exception:" + result.toString());
            LogUtil.error("querySpiritualityContent", e);
        }
        return content;
    }

    /**
     * 关键字匹配
     *
     * @param content
     * @param keyword
     * @return
     */
    public static String textMacth(String content, String keyword) {
        return textMacth(content, keyword, false);
    }

    /**
     * 关键字匹配
     *
     * @param content
     * @param keyword
     * @return
     */
    public static String textMacth(String content, String keyword, boolean searchTitle) {
        String[] keyWords = keyword.split(" ");
        List<String> strList = Arrays.asList(keyWords);
        List arrList = new ArrayList(strList);
        Iterator<String> stringIterator = arrList.iterator();
        while (stringIterator.hasNext()) {
            String string = stringIterator.next();
            if (TextUtils.isEmpty(string)) {
                stringIterator.remove();
            }
        }
        keyWords = new String[arrList.size()];
        arrList.toArray(keyWords);
        String result = content;
        if (searchTitle) {
            Document document = Jsoup.parse(content);
            Elements elements = document.getElementsByTag("b");
            for (Element element : elements) {
                int j = keyWords.length;
                for (int i = 0; i < j; i++) {
                    String kd = keyWords[i];
                    if (element.text().contains(kd)) {
                        result = result.replace(element.text(),
                                element.text().replace(kd, "<font color='#ff0000'>" + kd +
                                        "</font>"));
                    }
                }
            }
            return result;
        }
        int j = keyWords.length;
        for (int i = 0; i < j; i++) {
            String kd = keyWords[i];
            if (content.contains(kd)) {
                result = result.replace(kd, "<font color='#ff0000'>" + kd + "</font>");
            } else if (content.toLowerCase().contains(kd)) {//内容小写才有适配
                result = result.replace(kd.toUpperCase(), "<font color='#ff0000'>" + kd.toUpperCase() + "</font>");
            } else if (content.toUpperCase().contains(kd)) {
                result = result.replace(kd.toLowerCase(), "<font color='#ff0000'>" + kd.toLowerCase() + "</font>");
            } else {
                result = null;
                break;
            }
        }
        return result;
    }

    /**
     * 简体转繁体
     *
     * @param content
     * @return
     */
    public static String jian2fan(String content) {
        if (TextUtils.isEmpty(content)) return "";
        String changeText = null;
        try {
            JChineseConvertor jChineseConvertor = JChineseConvertor.getInstance();
            changeText = jChineseConvertor.s2t(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return changeText;
    }

    public static String replaceTag(String str1, String str2) {
        String str = str2.replace("", "");
        return Pattern.compile(str1).matcher(str).replaceAll("");
    }

    public static String getChapterTxtName(String volumeId, String indexId) {
        return "chapters/" + volumeId + "_" + indexId + ".txt";
    }

    /**
     * @param mCcontext
     * @param chapter
     * @param mTextModel
     * @param flag1      1是怀著带中文
     * @return
     */
    public static List<String> queryChaptreContent(Context mCcontext, Chapter chapter, int mTextModel, int flag1) {
        List<String> content = new ArrayList<String>();
        String contents = chapter.getContent();

        content = new ArrayList<String>(Arrays.asList(contents.split("\n")));

        // int remove = 0;
        for (int i = 0; i < content.size(); i++) {
            String string = content.get(i);
            if (string.trim().equals("\n") || string.trim().equals("\n\r") || StringUtil.isEmpty
                    (string.trim())) {
                content.remove(i);
                i--;
                continue;
            }
            if (mTextModel == SystemConfig.TEXT_MODEL_FANTI) {
                content.set(i, jian2fan(string + "\n"));
            }
        }

        LogUtil.debug("queryChaptreContent", "  content =  " + content);

        List<String> rContent = new ArrayList<String>();
        List<String> allTags = HuDongApplication.HZ_baseVersions;
        List<String> tags = HuDongApplication.mVersions_HZ;
        if (tags.size() != allTags.size()) {
            if (tags.get(0).contains("中文")) {
                for (String entity : content) {
                    if (entity.contains("中文")) {
                        rContent.add(entity);
                    }
                }
            } else { //英文
                for (String entity : content) {
                    if (entity.contains("英文")) {
                        rContent.add(entity);
                    }
                }
            }
            if(rContent.size() ==0){
                return content;
            }
            return rContent;
        }

        return content;
    }
}
